<?
require_once 'BaseAction.php';

class ViewController extends TT_Base_Action
{   
    
    public function replace_1($str)
        {
            $str1=trim($str);
            $str1=" $str1 ";
            $str2=str_replace(' ','%%',$str1);
            return $str2;
        } 

    public function indexAction()
        {
            $u = $this->getUser();
            $this->view->user=$u;
                                 $params =  $this->_getAllparams() ;
                                 $this->view->params=$params;

            if ($u->uid == 0)
                {            //chuyen ve trang login neu chua dang nhap  
                    $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                } 

 
                $a_model=$this->loadModel('Admins');               
                $check_moderator=$a_model->checkmoderator($u->uid,4);             // kiem tra co phai mod ko 
                $this->view->check_moderator=count($check_moderator);
                
             $u_model = $this->loadModel("Views");  
            if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                { 
                     
                     $params =  $this->_getAllparams();
                     $search = $params['search'];
                   
                     
                     $link="/view?x=".$search."&page=1";
                     $this->_helper->getHelper('Redirector')->gotoUrl($link);
                }
                    
                     $params =  $this->_getAllparams();

                     $search = $params['x'];
                     if($params['x']=='')
                     {
                       $search="";  
                     }
                     
                     $page_start=($params['page']-1)*5;
                     $page_rows=5;
                     if($params['page']=='')
                        {
                            $page_start=1;
                        }
                  
                     $this->view->search1=$search;
                     $a=$this->replace_1($search);
                     
                     $b=$u_model->search($a,"%%",$page_start,$page_rows); // tim` tu` bang user_info
                     $n=count($b);
                     $num=$u_model->num_search($a,"%%");
                     $this->view->num=$num;

                     $this->view->n=$n;// dua ra so ket qua tim kiem
                     $this->view->result=$b;   
                     
              
                          
        }
    public function userAction()
        {
            $u = $this->getUser();
            $this->view->user=$u;    

                $a_model=$this->loadModel('Admins');               
                $check_moderator=$a_model->checkmoderator($u->uid,4);             // kiem tra co phai mod ko 
                $this->view->check_moderator=count($check_moderator);
                    
            if ($u->uid == 0)
                {            //chuyen ve trang login neu chua dang nhap  
                    $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                } 
            $u_model = $this->loadModel("Views");  
             $params =  $this->_getAllparams(); 
             $this->view->params=$params;   
             $uid =$params['uid'];
            $user_name= $u_model->checkusername($uid);
             $this->view->user_name=$user_name;
             
             $info=$u_model->checkInfo($uid);
             $this->view->info=$info;
             
             if(!is_dir('profiles/'.$params['uid']))           // tao thu muc chua file neu thu muc chua ton tai
                     {
                     mkdir('profiles/'.$params['uid']);
                     }
            
                $dir="profiles/".$params['uid'];    
                $file=scandir($dir);
                $this->view->file=$file; 
             
             if($params['edit']==1) // neu nguoi dung an vao edit o? trang tim kiem ...
                {
                    $link="/admin/edit?user_name=".$user_name->name;
                   $this->_helper->getHelper('Redirector')->gotoUrl($link);
                }
             
             if($params['del']==1) // neu nguoi dung an vao edit o? trang tim kiem ...
                {
                    $link="/admin/deleteuser?x=".$user_name->name;
                   $this->_helper->getHelper('Redirector')->gotoUrl($link);
                }
        }
        
        
  
        
        
    public function allAction()
        {
            $u = $this->getUser();
            $this->view->user=$u;
                $a_model=$this->loadModel('Admins');               
                $check_moderator=$a_model->checkmoderator($u->uid,4);             // kiem tra co phai mod ko 
                $this->view->check_moderator=count($check_moderator);
                
            if ($u->uid == 0)
                {            //chuyen ve trang login neu chua dang nhap  
                    $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/user/login');
                } 
            $u_model = $this->loadModel("Views"); 
             
            $params =  $this->_getAllparams();
            $this->view->params=$params;
            $phong_ban=$params['phong_ban'];


            $v_model = $this->loadModel("Admins");
            $p=$v_model->phong_ban(); // lay danh sach cac phong ban
            $this->view->phong=$p;
            
            if ($this->getRequest()->isPost()) //neu nguoi dung submit form 
                {
                    $nghi=$params['nghi'];
                    $chon=$params['chon_phong_ban'];
                    if($nghi==1)
                    {
                        $link="/view/all?nghi=1&phong_ban=".$chon."&page=1";
                    }
                    else
                    {
                        $link="/view/all?phong_ban=".$chon."&page=1";
                    }
                    
                    $this->_helper->getHelper('Redirector')->gotoUrl($link);
                }
            
                                 $phong_ban_id=$params['phong_ban'];
                                 if($params['phong_ban']=='')
                                    {
                                        $phong_ban=0;
                                        $phong_ban_id=0;
                                    }
                                 
                                  if($phong_ban_id==0)
                                    {
                                        $phong_ban1="%%";
                                    }
                                  else
                                    {
                                        $phong_ban1=$u_model->phong_ban_id($params['phong_ban']);
                                        
                                        $this->view->view_phong_ban=$phong_ban1[0]->phong_ban;
                                        $phong_ban1=$phong_ban1[0]->phong_ban;
                                    }
                                 
                                 $this->view->test1=$params['phong_ban'];   
                                 
                                 if($params['page']=='')
                                    {
                                        $pages=1;
                                    }
                                 else
                                    {
                                        $pages=$params['page'];
                                    }
                                
                                 $this->view->phong_ban=$phong_ban;
                                 $page_start=($pages-1)*5;
                                 $page_rows=5;



                                
   
                                 $va3=$u_model->search3('%%',$phong_ban1,$page_start,$page_rows); //lay ket qua cua nhan vien dang lam viec
                                 $n3=$u_model->num_search3('%%',$phong_ban1);
                                 $this->view->n3=$n3;
                                 $this->view->result3=$va3;

                                 
   
                                 $va2=$u_model->search2('%%',$phong_ban1,$page_start,$page_rows); //lay ket qua cua nhan vien da nghi
                                 $n2=$u_model->num_search2('%%',$phong_ban1);
                                 $this->view->n2=$n2;
                                 $this->view->result2=$va2;
            }

          
        
}